package com.woniuxy.opencv.service.impl;

import com.woniuxy.opencv.pojo.ParkingOrder;
import com.woniuxy.opencv.pojo.ParkingRecord;
import com.woniuxy.opencv.service.AlipayService;
import com.woniuxy.opencv.service.ParkingOrderService;
import com.woniuxy.opencv.service.ParkingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private ParkingOrderService parkingOrderService;

    @Autowired
    private ParkingRecordService parkingRecordService;

    @Autowired
    private LockService lockService;



    @Override
    public String createPayment(ParkingOrder parkingOrder) {
        try {
            String url = "mock://pay/" + parkingOrder.getId();
            return url;
        } catch (Exception e) {
            log.error("创建支付订单异常 - 订单号: " + parkingOrder.getId(), e);
            return null;
        }
    }

    @Override
    public boolean queryPaymentStatus(String orderId) {
        // 查询数据库订单
        ParkingOrder order = parkingOrderService.getById(Long.parseLong(orderId));
        return order != null && order.getDoPay() != null && order.getDoPay() == 1;
    }

    @Override
    public void mockPaymentSuccess(String orderId) {
        // 更新订单状态
        parkingOrderService.updateOrderStatus(orderId, 1); // 1表示已支付
        try {
            Long orderIdLong = Long.parseLong(orderId);
            ParkingRecord record = parkingRecordService.getByOrderId(orderIdLong);
            if (record != null && record.getParkingId() != null) {
                lockService.handleLockOperation(record.getParkingId());
                log.info("已调用开锁服务，lockId: {}", record.getParkingId());
            } else {
                log.warn("未找到对应的停车记录或parkingId为空，无法开锁，orderId: {}", orderId);
            }
        } catch (Exception e) {
            log.error("开锁操作异常，orderId: {}", orderId, e);
        }
    }
} 